package com.wwwarehouse.contract.adjust_contract;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.fragment.DeskDrawerSeriesFragment;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.AdjustOrderBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ConfirmationEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static com.wwwarehouse.common.constant.Constant.KEY_CARD_DESK_FUNCTION_DETAILS;


/**
 * Creator by zhenwang.xiang on 2017/10/20 13:51
 * Description: 调整订单的viewpager
 */

@Route(path = ContractConstant.PATH_ADJUST_ORDER_VIEWPAGERFRAGMENT)
public class AdjustOrderViewPagerFragment extends CommonBasePagerFragment {
    private List<SeriesBean> mFilterList;

    private String businessId;
    private String platformId = "";
    private String businessType = "";
    private String sort = "a.contract_time desc";
    private String rsName = "";
    private int mSelectedPosition = 2;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (getArguments() != null) {
            CardDeskFunctionResponseBean.TaskBean taskBean = (CardDeskFunctionResponseBean.TaskBean) getArguments().getSerializable(KEY_CARD_DESK_FUNCTION_DETAILS);
            businessId = taskBean.getBusinessId();
        }


        mFilterList = new ArrayList<>();

        List<FilterBean> filterBean1 = new ArrayList<>();
        filterBean1.add(new FilterBean(false, "淘宝"));
        filterBean1.add(new FilterBean(false, "网仓"));

        mFilterList.add(new SeriesBean("来源", filterBean1, 3, false));


        List<FilterBean> filterBean2 = new ArrayList<>();
        filterBean2.add(new FilterBean(false, "供货契约"));
        filterBean2.add(new FilterBean(false, "订购契约"));

        mFilterList.add(new SeriesBean("类型", filterBean2, 2, false));

        DeskDrawerSeriesFragment deskDrawerSeriesFragment = DeskDrawerSeriesFragment.newInstance(mFilterList);
        ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerSeriesFragment);

        //网络请求
        requestHttp(businessType, platformId, rsName, sort);
    }

    //排序
    @Override
    public void sortClick(View anchor) {
        super.sortClick(anchor);
        if (peekFragment() instanceof AdjustOrderViewPagerFragment) {
            PopUpUtils.showBubbleInviteSort(mActivity, anchor, mSelectedPosition, new PopUpUtils.OnChoiceListener() {
                @Override
                public void onChoice(int position) {
                    mSelectedPosition = position;
                    switch (position) {
                        case 2:
                            sort = "a.contract_time desc";
                            break;
                        case 3:
                            sort = "c.total_amount desc";
                            break;
                        default:
                            sort = "a.contract_time desc";
                            break;
                    }
                    requestHttp(businessType, platformId, rsName, sort);
                }
            }, null, getString(R.string.adjust_order_sort_time), getString(R.string.adjust_order_sort_money));
        }
    }

    //过滤
    @Override
    public void filterClick() {
        super.filterClick();
        if (peekFragment() instanceof AdjustOrderViewPagerFragment) {
            ((BaseCardDeskActivity) mActivity).showDrawerLayout();
        }
    }

    /**
     * Creator by zhenwang.xiang on 2017/10/27 19:18
     * Description: 网络请求 默认requestHttp(0,0,"","");
     */
    private void requestHttp(String businessType, String platformId, String name, String sort) {
        Map<String, Object> map = new HashMap<>();
        map.put("businessId", businessId);
        map.put("page", "1");
        map.put("size", "5");
        map.put("sort", sort);
        if (!TextUtils.isEmpty(businessType)) {
            map.put("businessType", businessType);
        }
        if (!TextUtils.isEmpty(platformId)) {
            map.put("platformId", platformId);
        }
        if (!TextUtils.isEmpty(name)) {
            map.put("rsName", name);
        }

        loadData(ContractConstant.GET_ADJUST_ORDERS, map, 0);

    }

    @Override
    public void setTitle() {
        super.setTitle();
        if (peekFragment() instanceof AdjustOrderViewPagerFragment) {
            mActivity.setTitle(getString(R.string.adjust_order_title));
        }
    }

    @Override
    public void onRefreshListener() {
        requestHttp(businessType, platformId, rsName, sort);
    }

    @Override
    public void onLoadSuccess(String data, int what) {
        if (0 == what) {
            AdjustOrderBean bean = JSON.parseObject(data, AdjustOrderBean.class);
            if (null != bean) {
                Bundle bundle = new Bundle();
                bundle.putString("businessId", businessId);
                bundle.putString("sort", sort);
                bundle.putString("businessType", businessType);
                bundle.putString("platformId", platformId);
                bundle.putString("rsName", rsName);
                try {
                    setData(bean.getTotal(), 5, "com.wwwarehouse.contract.adjust_contract.AdjustOrderFragment", bundle, bean.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onLoadError(String errorInfo) {
        ToastUtils.showToast(errorInfo);
    }

    //搜索
    @Override
    public void textChanged(String text) {
        super.textChanged(text);
        rsName = text;
        requestHttp(businessType, platformId, rsName, sort);
    }

    //取消
    @Override
    public void cancelClick() {
        super.cancelClick();
        rsName = "";
        requestHttp(businessType, platformId, rsName, sort);
    }

    public void onEventMainThread(Object object) {
        if (object instanceof ConfirmationEvent) {
            String msg = ((ConfirmationEvent) object).getMsg();
            if (msg.equals("refresh")) {
                requestHttp(businessType, platformId, rsName, sort);
            }
        } else if (object instanceof DrawerSeriesCompleteEvent) {
            if (peekFragment() instanceof AdjustOrderViewPagerFragment) {
                List<SeriesBean> seriesBeanList = ((DrawerSeriesCompleteEvent) object).getList();
                boolean reset = ((DrawerSeriesCompleteEvent) object).isReset();
                List<FilterBean> beanList1 = seriesBeanList.get(0).getList();
                for (int i = 0; i < beanList1.size(); i++) {
                    platformId = "";
                    if (beanList1.get(0).isSelect()) {
                        platformId = "10";
                    }
                    if (beanList1.get(1).isSelect()) {
                        platformId = "200";
                    }
                    if (beanList1.get(0).isSelect() && beanList1.get(1).isSelect()) {
                        platformId = "10,200";
                    }
                }
                List<FilterBean> beanList2 = seriesBeanList.get(1).getList();
                for (int i = 0; i < beanList2.size(); i++) {
                    businessType = "";
                    if (beanList2.get(0).isSelect()) {
                        businessType = "2";
                    }
                    if (beanList2.get(1).isSelect()) {
                        businessType = "1";
                    }
                    if (beanList2.get(0).isSelect() && beanList2.get(1).isSelect()) {
                        businessType = "";
                    }
                }
                if (!reset) {
                    requestHttp(businessType, platformId, rsName, sort);
                }

            }
        }
    }
}
